package com.tbit.common.entity.vo;

import cn.hutool.core.util.StrUtil;
import lombok.Data;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 运维报表数据参数实体类
 *
 * @author: qiu.longjie
 * @create: 2025-05-08
 */
@Data
public class OperationReportReqVo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 区域id集合，多个以英文逗号,拼接
     */
    @NotEmpty(message = "区域id不能为空")
    private String accountIds;

    /**
     * 数据日期，int类型，格式为yyyyMMdd，例如20180508
     */
    @Min(value = 10000101, message = "数据日期不能小于10000101")
    @Max(value = 99991231, message = "数据日期不能大于99991231")
    @NotNull(message = "数据日期不能为空")
    private Integer dataDate;

    /**
     * 获取区域id集合
     */
    public List<Integer> getAccountIdList() {
        if (StrUtil.isNotEmpty(accountIds)) {
            return Arrays.stream(accountIds.split(",")).map(Integer::valueOf).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    /**
     * 获取dataDate n天前的日期，格式为yyyyMMdd
     */
    public Integer getDaysAgo(int days) {
        if (dataDate == null) {
            return null;
        }
        return Integer.parseInt(
                LocalDate.parse(String.format("%08d", dataDate), DateTimeFormatter.BASIC_ISO_DATE)
                        .minusDays(days)
                        .format(DateTimeFormatter.BASIC_ISO_DATE)
        );
    }
}
